<template>
    <div class="accountWrap">
            <div class="title">我的资质</div>
            <el-form ref="form" :model="form" label-width="80px" :disabled="isWrite">
                <el-form-item label="终端商名称" :rules="[{ required: true, message: '不能为空'}]">
                    <el-input v-model="form.company_full_name"></el-input>
                </el-form-item>
                <el-form-item label="终端商简称" :rules="[{ required: true, message: '不能为空'}]">
                    <el-input v-model="form.company_short_name"></el-input>
                </el-form-item>
                <el-form-item label="申请人姓名" :rules="[{ required: true, message: '不能为空'}]">
                    <el-input v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="申请人职务(可选)">
                    <el-input v-model="form.job"></el-input>
                </el-form-item>
                <el-form-item label="申请人联系电话" :rules="[{ required: true, message: '不能为空'}]">
                    <el-input v-model="form.phone"></el-input>
                </el-form-item>
                <el-form-item label="申请人邮箱" :rules="[{ required: true, message: '不能为空'}]">
                    <el-input v-model="form.email"></el-input>
                </el-form-item>
                <el-form-item label="申请人联系地址" :rules="[{ required: true, message: '不能为空'}]">
                    <el-input v-model="form.addr"></el-input>
                </el-form-item>
                <el-form-item label="公司地址(可选)">
                    <el-input v-model="form.company_addr"></el-input>
                </el-form-item>   
                <div class="logoWrap">             
                <el-form-item label="LOGO(可选)">
                    <img src="@/static/img/default.png" v-if="!form.logo" class="logoImg" alt="">
                    <img :src="form.logo" v-if="form.logo" class="logoImg" alt="">
                    <div class="input-group" v-if="!isWrite"><div class="input-group-addon"><label style="position:relative;"><img src="@/static/img/image.jpg" title="send images" style="cursor:pointer;"><input id="sendImage" type="file" value="images" style="display:none;"  onchange="window.preview()"></label></div></div>
                </el-form-item>
                </div>
                <div class="onSubmit">
                    <el-form-item>
                        <el-button type="primary" @click="onSubmit"><i class="el-icon-upload"></i>  提交</el-button>
                    </el-form-item>
                </div>
            </el-form>
    </div>
</template>

<script>
    import $ from 'jquery'
    import _ from 'underscore'
    export default {
        data() {
            return {
                form:{
                    token:localStorage.getItem('token'),
                    company_full_name:'',
                    company_short_name:'',
                    name:'',
                    job:'',
                    phone:'',
                    email:'',
                    addr:'',
                    company_addr:'',
                    logo:''
                },
                isWrite:false
            }
        },
        mounted(){
            window.preview=()=>{
                var img = new Image();
                var file=$('#sendImage')[0].files[0]
                var reader=new FileReader()
                reader.onloadend=(e)=>{
                    var data = e.target.result;
                    img.src=data
                }
                reader.readAsDataURL(file)
                img.onload = ()=> {
                    var w = Math.min(150, img.width);
                    var h = img.height * (w / img.width);
                    var canvas = document.createElement('canvas');
                    var ctx = canvas.getContext('2d');

                    // 设置 canvas 的宽度和高度
                    canvas.width = w;
                    canvas.height = h;

                    // 把图片绘制到 canvas 中
                    ctx.drawImage(img, 0, 0, w, h);
                    // 取出 base64 格式数据
                    this.form.logo = canvas.toDataURL(file.type);
                    $('.logoImg').attr('src',this.form.logo)
                }
            }
        },
        activated(){
             $('.el-button--primary')[0].setAttribute("style","background-color: rgb(58, 19, 90) !important;border-color: rgb(58, 19, 90) !important;margin: 12px auto !important;")
            this.checkWriteInfo()
        },
        computed: {
          
        },
        methods: {
            onSubmit(){
                this.$confirm('是否确认信息无误(只能提交一次)?', '提示', {
                  confirmButtonText: '确定',
                  cancelButtonText: '取消',
                  type: 'warning'
                }).then(() => {
                    var data=[this.form.name,this.form.phone,this.form.company_full_name,this.form.company_short_name,this.form.addr]
                    var hasEmpty=false
                    _.each(data,(value)=>{
                        if($.trim(value)==''){
                            hasEmpty=true
                        }
                    })
                    if(hasEmpty){
                        this.$message.error({message:'必填信息不能为空',duration:1000});
                        return
                    }
                    this.$http.post(this.$store.getters.serverIp+'edit_information',this.form).then((response)=>{
                        if(response.data.success){
                            this.$message({message:response.data.info,duration:1000})
                            localStorage.setItem('isWrite','isWrite')
                            this.isWrite=true
                        }else{
                            this.$message.error({message:response.data.info,duration:1000})
                        }
                    }).catch((response)=>{

                    })
                }).catch(() => {
                  this.$message({
                    type: 'info',
                    message: '已取消提交'
                  });          
                });
                setTimeout(()=>{
                    $('.el-button--primary')[1].setAttribute("style","background-color: rgb(58, 19, 90) !important;border-color: rgb(58, 19, 90) !important;")
                },10)
            },
            checkWriteInfo(){
                this.$http.post(this.$store.getters.serverIp+'get_information',{token:localStorage.getItem('token')}).then((response)=>{
                    if(response.data.success){
                        if(response.data.userinfo.head){
                            localStorage.setItem('userImg',this.$store.getters.serverUserImgStaticIp+response.data.userinfo.head)
                            this.$store.commit('setUserImg',this.$store.getters.serverUserImgStaticIp+response.data.userinfo.head)
                        }
                        var flag=_.isEmpty(response.data.company)
                        if(!flag){//己编辑
                            var obj={}
                            _.extendOwn(obj,response.data.userinfo,response.data.company)
                            obj.company_short_name=obj.short_name
                            obj.company_full_name=obj.full_name
                            obj.logo=this.$store.getters.serverStaticIpLogo+obj.logo
                            console.log(obj.logo)
                            this.form=obj
                            this.isWrite=true
                        }else{
                            localStorage.setItem('isWrite','noWrite')
                        }
                    }
                }).catch((response)=>{

                })
            },
        }
    }

</script>

<style lang="less" >
    .accountWrap{
        .el-form{
            width: 78%;
            margin: 0 auto;
            margin-top: 25px;
        }
        .onSubmit{
            .el-form-item--small{
                border-top: 1px solid #ccc !important;
                text-align: center !important;
                width: 100% !important;
                margin-top: 60px !important;
            }
            .el-form-item__content{
                margin: 0 !important;
            }
            .el-button--primary{
                margin-top: 10px !important;
            }
        }
        .el-form-item__label{
            position: relative !important;
            top: 33px !important;
            width: 123px !important;
            left: -42px;
        }
        .el-form-item{
            width: 45%;
            margin: 0 auto;
        }
        .logoImg{
            margin-top: 19px;
            max-width:120px;
        }
        .input-group{
            display: inline-block;
            border-collapse: separate;
            position: absolute;
            top: 55%;
            right: 53%;
            .input-group-addon {
                padding: 4px 5px;
                font-size: 14px;
                font-weight: 400;
                line-height: 1;
                color: #555;
                text-align: center;
                background-color: #c0c0c0;
                border-radius: 4px;
                label {
                    display: inline-block;
                    max-width: 100%;
                    margin-bottom: 5px;
                    font-weight: 700;
                    img {
                        vertical-align: middle;
                        cursor: pointer;
                    }
                    input{
                        display: none;
                    }
                }
            }
        }
    }
</style>
